<?php

/**
 * Hoa Framework
 *
 *
 * @license
 *
 * GNU General Public License
 *
 * This file is part of HOA Open Accessibility.
 * Copyright (c) 2007, 2008 Ivan ENDERLIN. All rights reserved.
 *
 * HOA Open Accessibility is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * HOA Open Accessibility is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with HOA Open Accessibility; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *
 * @category    Framework
 * @package     Hoathis_Convertor
 *
 */

/**
 * Hoa_Framework
 */
require_once 'Framework.php';

/**
 * Hoathis_Convertor_Format_Abstract
 */
importModule('Convertor.Format.Abstract');

/**
 * Class Hoathis_Convertor_Format_Radio
 *
 * Extending the Hoathis_Convertor_Format_Abstract class.
 *
 * @author      Grégory DARCHE <tetardo@gmail@gmail.com>
 * @copyright   Copyright (c) 2009 Grégory DARCHE.
 * @license     http://gnu.org/licenses/gpl.txt GNU GPL
 * @since       PHP 5
 * @version     0.1
 * @package     Hoathis_Convertor
 * @subpackage  Hoathis_Convertor_Format
 */
Class Hoathis_Convertor_Format_Radio extends Hoathis_Convertor_Format_Abstract {

    protected $units = array(
        'txt'	=> null,
        'radio' => null
    );

    protected $filters = array(
        'Censure'
    );

	protected $table1 = array("0","1","2","3","4","5","6","7","8","9",
	"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z",
	"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","Z","Y","Z",
	",","."," ","?","'","!","/","(",")","&",":",";","=","+","-","_","\"","$","@","€");

	protected $table2 = array("Zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine",
	"alpha","bravo","charlie","delta","echo","foxtrot","golf","hotel","india","juliet","kilo","lima",
	"mike","november","oscar","papa","quebec","romeo","sierra","tango","uniform","victor","whisky","x-ray",
	"yankee","zulu","ALPHA","BRAVO","CHARLIE","DELTA","ECHO","FOXTROT","GOLF","HOTEL","INDIA","JULIET",
	"KILO","LIMA","MIKE","NOVEMBER","OSCAR","PAPA","QUEBEC","ROMEO","SIERRA","TANGO","UNIFORM","VICTOR",
	"WHISKY","X-RAY","YANKEE","ZULU","Decimal","Stop","|","?","\'","!","/","(",")","&",":",";","=","+",
	"-","_","\"","$","@","€");

	protected $separator = ' ';

    protected function convert( $value, $from, $to ) {

        switch($from) {

            case 'txt':
                return $this->alphaToRadio($value);
                break;

            case 'radio':
                return $this->radioToAlpha($value);
                break;
        }
    }

	private function alphaToRadio($str) {

        $str = Hoathis_Convertor::getInstance('accent', $str, 'accent', 'noaccent');
        $strcode = '';

       for ($i=0;$i<strlen($str);$i++) {

          for ($j=0;$j<sizeof($this->table1);$j++) {

             if ($this->table1[$j]==substr($str,$i,1)) {

                $strcode .= $this->table2[$j];

                if ($i!=strlen($str)-1) {

                   $strcode .= $this->separator;
	            }
	         }
	      }
	   }

       return $strcode;
	}

	private function radioToAlpha($str) {

	   $tablestr = explode($this->separator, $str);
	   $strdecode = '';

       for ($i=0;$i<sizeof($tablestr);$i++) {

          for ($j=0;$j<sizeof($this->table2);$j++) {

             if ($this->table2[$j]==$tablestr[$i]) {

                $strdecode .= $this->table1[$j];
	         }
	      }
	   }

       return $strdecode;
	}
}